//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <stdio.h>
#include <winhack.h>
#include <LogUtility.h>
#include <sysconfig.h>
#include <elasys_server.h>

#define LogUtility_Switch L"LogSwitch"
#define LogUtility_Level  L"LogLevel"

typedef struct LogPrintCfg {
    Int32 printToWhere;
    Int32 level;
}LogPrintCfg;

static LogPrintCfg logPrintCfg = {LOG_OFF, LEVEL_ERROR};

EXTERN_C Int32 __cdecl AuraLog_print(Int32 level, const char *fmt, ...)
{
    va_list args;
    Int32 len = 0;

    switch (logPrintCfg.printToWhere) {
        case LOG_TO_LOCAL_CONSOLE:
            if (level >= logPrintCfg.level) {
                va_start(args, fmt);
                len = vprintf(fmt, args);
                va_end(args);
            }
            break;
        case LOG_TO_REMOTE_CONSOLE:
    	case LOG_OFF:
        default:
            break;
    }
    return len;
}

EXTERN_C BOOL  __stdcall DllMain(HANDLE hModule, DWORD dwReason, PVoid lpReserved)
{
    switch (dwReason) {
        case DLL_PROCESS_ATTACH:
            ConfigGetItemInt(LogUtility_Switch, &logPrintCfg.printToWhere);
			ConfigGetItemInt(LogUtility_Level, &logPrintCfg.level);
			break;
        case DLL_PROCESS_DETACH:
            break;
    }
    return TRUE;
}
